package com.crm.controller.before_sale;

import com.crm.pojo.ClientNeed;
import com.crm.pojo.SaleChance;
import com.crm.service.before_sale.ClientNeedService;
import com.crm.vo.ClientNeedVO;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class NeedController {
    @Autowired
    private ClientNeedService clientNeedService;
    //客户需求表
    @GetMapping("zjk-need")
    public String tosaleChanceDatagrid(){ return "before-sale/need";}
    //编辑客户需求表单
    @GetMapping("zjk-edit-need-form")
    public String toEditNeedFrom(){
        return "before-sale/edit-need-form";
    }
    //添加客户需求表单
    @GetMapping("zjk-need-form")
    public String toNeedFrom(){
        return "before-sale/need-form";
    }
    //客户需求详情表单（仅查看）
    @GetMapping("zjk-need-details")
    public String toNeedDetails(){
        return "before-sale/need-details";
    }

    /**
     * 分页展示所有客户需求
     * @param clientNeed
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @PostMapping("showClientNeedList")
    @ResponseBody
    public Map showClientNeedList(ClientNeed clientNeed, int page, int rows, HttpServletRequest request){
        List<ClientNeed> list = clientNeedService.showClientNeedList(clientNeed,page,rows,null,null);
        Long total = clientNeedService.getClientNeedCount(clientNeed,null,null);
        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return  map;
    }

    /**
     * 客户需求高级查询
     * @param clientNeed
     * @param page
     * @param rows
     * @param recodeStartTime
     * @param recodeEndTime
     * @return
     */
    @PostMapping("advance-ClientNeed")
    @ResponseBody
    public Map advanceClientNeed(ClientNeed clientNeed,int page,int rows,Date recodeStartTime,Date recodeEndTime){
        Long total = clientNeedService.getClientNeedCount(clientNeed,recodeStartTime,recodeEndTime);
        List<ClientNeed> list = clientNeedService.showClientNeedList(clientNeed,page,rows,recodeStartTime,recodeEndTime);
        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }




    /**
     * 删除客户需求
     * @param needId
     * @return
     */
    @PostMapping("deleteClientNeed")
    @ResponseBody
    public Result deleteClientNeed(String  needId){
        Long id = needId == null ? 0:new Long(needId);
        return clientNeedService.deleteClientNeed(id);

    }

    /**
     * 添加一个客户需求
     * @param needTheme
     * @param clientId
     * @param contactId
     * @param chanceId
     * @param recordTime
     * @param importance
     * @param needContent
     * @return
     */
    @PostMapping("zjk-addClientNeed")
    @ResponseBody
    public Result addClientNeed(String needTheme, BigDecimal clientId,BigDecimal contactId,
                                BigDecimal chanceId,Date recordTime,String importance,String needContent){
        try {
            return clientNeedService.addClientNeed(needTheme,clientId,contactId,chanceId,recordTime,importance,needContent);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(e);
        }


    }

    /**
     * 编辑客户需求信息
     *
     * @return
     */
    @PostMapping("zjk-edit-clientNeed")
    @ResponseBody
    public Result editClientNeed(ClientNeed clientNeed){
        Result result = clientNeedService.editClientNeed(clientNeed);
        return result;
    }
}
